' This is a sample GradeBook Program
' Run it from Console
' This program opens grade.txt file where the students' name and their grades are saved
' be sure to save the grade.txt file in the debug subfolder in your project
' Each line of grade.txt is in this format: eg: 'Simon Crown 78'
' Program will show the name and grades, Average grades, Maximum and Minimum grade
' It will also allow us to add additional name and grades
' Assumption one is Name has only first and last name.
' Second asumption is that put the cursor in the starting of a new line, otherwise it will write the new added line behind the existing last line.

Imports System.IO           ' This is for the console use



Module GradeBook

    Dim strNamesAndGrades() As String 'Read the gradebook file into an array
    Dim strNames() As String          'will store the names of the students
    Dim intGrades() As Integer            'will store the grades of the students

    Sub GetstrNamesAndGrades()
        strNamesAndGrades = IO.File.ReadAllLines("grade.txt") 'Read the gradebook file into an array
        ReDim strNames(strNamesAndGrades.GetUpperBound(0))           'will store the names of the students
        ReDim intGrades(strNamesAndGrades.GetUpperBound(0))             'will store the grades of the students

        'get the names and grades
        For i = 0 To strNamesAndGrades.GetUpperBound(0)
            strNames(i) = strNamesAndGrades(i).Substring(0, strNamesAndGrades(i).Length - 3)
            intGrades(i) = strNamesAndGrades(i).Substring(strNamesAndGrades(i).Length - 2, 2)
        Next i
    End Sub


    ' Informations(): to show the options list to the user

    Sub Informations()
        Console.WriteLine("")
        Console.WriteLine("")
        Console.WriteLine(">Welcome to the Gradebook program!")
        Console.WriteLine("-----------------------------------------------------------------------------") 'Creates a line to make the program ordered
        Console.WriteLine("> Please choose one of the following options:")
        Console.WriteLine("-----------------------------------------------------------------------------")
        Console.WriteLine("")
        Console.WriteLine("Display Names and intGrades(1)") ' Displays the options you have to choose to start the program
        Console.WriteLine("Display Just strNames(2)")
        Console.WriteLine("Calculate Average (3)")
        Console.WriteLine("Maximum (4)")
        Console.WriteLine("Minimum (5)")
        Console.WriteLine("Draw Graph (6)")
        Console.WriteLine("Add Item (7)")
        Console.WriteLine("Delete Item (8)")
        Console.WriteLine("Quit (9)")

        Console.WriteLine("-----------------------------------------------------------------------------")
        Console.Write("Your option:" & "")                                      'uses write so that the number you put is on the same line as it
    End Sub

    ' The program starts from here

    Sub Main()

        Dim strUserInput As String                                    'Number that is chosen
        GetstrNamesAndGrades()                                   'get the grades into memory
        Do
            Informations()                                      ' Show the options list after each operation
            strUserInput = Console.ReadLine                      'Get the number until it is 8
            Console.Clear()
            Select Case strUserInput

                Case "1"
                    option1()                                   'Display Name and Grades
                Case "2"
                    option2()                                   'Display Name only
                Case "3"
                    option3()                                   'Display Calculated Average
                Case "4"
                    option4()                                   'Display Calculated Maximum
                Case "5"
                    option5()                                   'Display Calculated Minimum
                Case "6"
                    option6()                                   'Display Graph with Names
                Case "7"
                    option7()                                   'Add Item to text file
                Case "8"
                    Option8()                                   'Delete Student
                    'notice how Case 9 is not needed since '9' breaks the loop
            End Select

        Loop Until strUserInput = 9                                    'Quit the program if 8 chosen as well
    End Sub

    'Option1() shows all the students name and grades

    Sub option1()
        For i = 0 To intGrades.Count - 1
            Console.WriteLine(strNames(i) & ": " & intGrades(i))
            'Displays Name and Grade of Student
        Next i
    End Sub

    'Option2() shows all the students name only

    Sub option2()
        For i = 0 To intGrades.Count - 1
            Console.WriteLine(strNames(i))
            'Displays Name Student
        Next i
    End Sub


    'calculate average
    Sub option3()
        Console.WriteLine("The Average is:" & CStr(Math.Round(intGrades.Average, 2)) & "%") 'Rounds average to 2 decimal places and Displays it
    End Sub

    'Calculate MAX
    Sub option4()
        Console.WriteLine("The Average is:" & CStr(Math.Round(intGrades.Max, 2)) & "%") 'Rounds Max to 2 decimal places and Displays it
    End Sub

    'calculate MIN
    Sub option5()
        Console.WriteLine("The Average is:" & CStr(Math.Round(intGrades.Min, 2)) & "%") 'Rounds Min to 2 decimal places and Displays it
    End Sub

    'Display Graph
    Sub option6()

        Dim bar As Integer
        For i = 0 To strNames.GetUpperBound(0)

            bar = Math.Round(intGrades(i) / 10, 0)                'Rounds the grade to nearest whole number
            Dim fmtStr As String = "{0,20}{1,-10}"              'Aligning the stars

            'put the stars together
            Dim Stars As String = ""
            For j = 0 To bar - 1
                Stars = Stars & "*"
            Next
            Console.WriteLine(fmtStr, strNames(i) & ":", Stars) 'Displays name and the bar graph of the grade
        Next i
    End Sub

    'Add a new student to the file
    Sub option7()
        Dim name, grade As String

        Dim sw As New IO.StreamWriter("grade.txt", True) 'There is a True at the end so that it doesnt wipe the whole file then write
        Console.Write("Name: ")                             'Asks for name
        name = Console.ReadLine()                           'Gets the name
        Console.Write("Grade: ")                            'Asks for Grade
        grade = Console.ReadLine()                          'Gets Grade
        sw.WriteLine(name & " " & grade)                    ' Writes name and grade to txt file
        sw.Close()                                          'Closes sw
    End Sub

    'delete the student from the file
    Sub Option8()

        Dim name As String
        Dim blnNameFound As Boolean = False

        Console.Write("Name: ")                             'Asks for name
        name = Console.ReadLine()                           'Gets the name

        Dim sw As New IO.StreamWriter("grade.txt", False) 'There is a False at the end so that it does wipe the whole file then write
        For i = 0 To strNames.GetUpperBound(0)

            'write the name/grade to the file if it does not match what the user entered
            If strNames(i) <> name Then
                sw.WriteLine(strNames(i) & " " & intGrades(i))
            Else
                blnNameFound = True
            End If
        Next i
        sw.Close()
        If Not blnNameFound Then
            Console.WriteLine("Name not found!")
        End If

        'the list in memory will be updated after this
        GetstrNamesAndGrades()                                   'get the grades into memory
    End Sub

End Module
